package com.gexin.controller.system.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.system.AdminDict;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/system/dict", viewPath = "WEB-INF/system/admin/")
public class AdminDictController extends SystemBaseController {

	// 查询字典数据
	public void index() {
		List<AdminDict> list = AdminDict.dao.find("select  id,busi_name,busi_value,parent_id,state,sort_order from  t_admin_dict where parent_id=0");
		for (AdminDict d : list) {
			d.put("sub",AdminDict.dao.find("select  id,busi_name,busi_value,parent_id,state,sort_order from  t_admin_dict where parent_id=?", d.getId()));
		}
		render("dict_list.html");
	}

	
	public void query() {
		String pid = getPara("pid", "0");
		Map<Object, Object> dataMap = new HashMap<Object, Object>();
		Page<AdminDict>  page =  AdminDict.dao.paginateDictByPidDictByPid(getpage(), getpageSize(), pid);
		for (AdminDict d : page.getList()) {
			d.put("sub",AdminDict.dao.find("select  id,busi_name,busi_value,parent_id,state,sort_order from  t_admin_dict where parent_id=? ORDER BY  sort_order desc", d.getId()));
		}
		dataMap.put("pageData", page);
		if (!pid.equals("0")) {
			AdminDict dict = AdminDict.dao.findById(pid);
			dataMap.put("dictInfo", dict);
		}
		renderJson(dataMap);
	}

	public void test(){
		renderJson(AdminDict.dao.cache_findDictByKey("LOG_TYPE"));
	}
	/**
	 * 删除
	 */
	public void delete() {
		String pid = getPara();
		if (AdminDict.dao.getDictByPid(pid).size() > 0) {
			fail("下面有子类，请先删除！");
			return;
		}
		AdminDict root_dict    = AdminDict.dao.findById(getPara());
		if(root_dict!=null && !root_dict.getParentId().equals("0")){
			root_dict    = AdminDict.dao.findById(root_dict.getParentId());
			AdminDict.dao.cache_claer_ByKey(root_dict.getBusiValue());
		}
		AdminDict.dao.deleteById(getPara());
		success();
	}

	// 获取详细信息
	public void info() {
		renderJson(AdminDict.dao.findById(getPara()));
	}

	/**
	 * 添加或者保存
	 */
	public void addsave() {
		AdminDict dict = getModel(AdminDict.class, "dict");
		if (dict.getState() == null) {
			dict.setState(2);
		} else {
			dict.setState(1);
		}
		if (dict.getId() == null) {
			// 添加
			dict.setId(IdGenerateUtils.getId());
			if (dict.getParentId() == null) {
				dict.setBusiLevel(1);
				dict.setParentId("0");
			} else {
				dict.setBusiLevel(2);
			}
			dict.save();
		} else {
			// 修改
			dict.update();
		}
		AdminDict root_dict    = AdminDict.dao.findById(dict.getParentId());
		if(root_dict!=null &&  root_dict.getParentId().equals("0")){
			AdminDict.dao.cache_claer_ByKey(root_dict.getBusiValue());
		}
		success();
	}

	/**
	 * 验证字典名称是否重复
	 * 
	 * @param value
	 */
	public void checkRepeti() {
		String value = getPara("value", null);
		String dict_id = getPara("dict_id", null);
		String  pid = getPara("pid", null);
		String  type = getPara("type", null);//检测的类型
		String  query_sql = type.equals("busi_name")?"busi_name=?":"busi_value=?";
		if (dict_id != null) {
			AdminDict adminDict = AdminDict.dao.findById(dict_id);
			if (adminDict.getBusiName().equals(value) || adminDict.getBusiValue().equals(value)) {
				fail();
			} else {
				List<AdminDict> list = AdminDict.dao.getByNameOrValue(query_sql,value,pid);
				if (list.size() > 0) {
					success();
				} else {
					fail();
				}
			}
		} else {
			List<AdminDict> list = AdminDict.dao.getByNameOrValue(query_sql,value,pid);
			if (list.size() > 0) {
				success();
			} else {
				fail();
			}
		}
	}
}
